﻿using Domain.Entitys;
using Domain.Repositorys;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFCore.Repositorys
{
    public class CFFKRepositoy : ICFFKRepositoy
    {
        private readonly MyDbContext myDbContext;

        public CFFKRepositoy(MyDbContext myDbContext)
        {
            this.myDbContext = myDbContext;
        }

        /// <summary>
        /// 删除1级机构表数据
        /// </summary>
        /// <param name="cFFK"></param>
        /// <returns></returns>
        public async Task<int> CFFKDeleteAsync(CFFK cFFK)
        {
            myDbContext.Remove(cFFK);
            return await myDbContext.SaveChangesAsync();
        }

        /// <summary>
        /// 添加1级机构表数据
        /// </summary>
        /// <param name="cFFK"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> CFFKInsertAsync(CFFK cFFK)
        {
            await myDbContext.CFFKs.AddAsync(cFFK);
            return await myDbContext.SaveChangesAsync();
        }

        /// <summary>
        /// 分页查询1级机构表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task<List<CFFK>> CFFKSelect(int pageIndex, int pageSize)
        {
            return await myDbContext.CFFKs.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();
        }

        /// <summary>
        /// 按id查询1级结构表数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<CFFK> CFFKSelectByIdAsync(int id)
        {
            return await myDbContext.CFFKs.FirstOrDefaultAsync(c => c.FfkId == id);
        }

        /// <summary>
        /// 按名称查询1级结构表数据
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<CFFK> CFFKSelectByName(string name)
        {
            return await myDbContext.CFFKs.FirstOrDefaultAsync(c => c.FirstKindId == name);
        }

        /// <summary>
        /// 查询数据总数
        /// </summary>
        /// <returns></returns>
        public int CFFKSelectCount()
        {
            return myDbContext.CFFKs.Count();
        }


        /// <summary>
        /// 查询全部1级机构表数据
        /// </summary>
        /// <returns></returns>
        public async Task<List<CFFK>> CFFKSelectOption()
        {
            return await myDbContext.CFFKs.ToListAsync();
        }

        /// <summary>
        /// 修改1级结构编号
        /// </summary>
        /// <param name="cFFK"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> CFFKUpdateAsync(CFFK cFFK)
        {
            myDbContext.Update(cFFK);
            return await myDbContext.SaveChangesAsync();
        }
    }
}
